package org.yuyao.gateway.filter;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 此配置是基于Redis的令牌桶算法限流
 */
@Configuration
public class RateLimiterConfig {
    @Bean
    public KeyResolver ipKeyResolver() {
        // 基于客户端 IP 限流
        return exchange -> Mono.just(
                exchange.getRequest().getRemoteAddress().getAddress().getHostAddress()
        );
    }


    //和上述代码功能一样
   /* @Bean
    public KeyResolver ipKeyResolver1() {
        // 基于客户端 IP 限流
      return  new KeyResolver(){
          @Override
          public Mono<String> resolve(ServerWebExchange exchange) {
              return Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());;
          }
      };
    }*/
}
